#!/bin/bash

#!/bin/bash

## the informal map-reduce shell scripts
## usage:
## l2m.sh <component1 component2]> <env> <localfile> <remotefile> <user>

comps=$1
env=$2
localfile=$3
remotefile=$4
user=$5

if [ "${comps}" = "" ];
then
echo "Usage: l2r.sh <component1 component2> <env> <localfile> <remotefile> <user>"
echo "	Use blank space to seperate the multiple components"
echo "	env: optional. Default env is ec2-cv-prod"
echo "	user: optional.Default user is administrator"
echo "	localfile: Specify the local file to be copied "
echo "	remotefile: Specify the remote file distinction to be copied "
exit
fi

if [ "$env" == "" ];
then
env=ec2-cv-prod
fi

if [ "${user}" == "" ];
then
user=administrator
fi

# build the server file for specified components

#prepare a work dir for this mapred task
if [ "${workdir}" == "" ];
then
workdir=./`date '+%ss'`
echo "INFO: use a random work dir - ${workdir}"
else
echo "INFO:	use the specified work dir - ${workdir}"
fi

./prepare.sh "${workdir}" "${scripts}" "$env" "${comps}"
serverfile="${workdir}"/server.txt
sshkeyfile="${workdir}"/ssh_key

for server in `cat ${serverfile}` ; do
echo "user - ${user}, copy local file - ${local}, to server - ${server}, remote file - ${remote}"
scp -i ${sshkeyfile} ${localfile} ${user}@${server}:"${remotefile}" &
done
